/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：OrderContorller.java
 * History:
 *       Apr 16, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseOrderInfo;
import com.xteam.ecsystem.service.admin.OrderService;
import com.xteam.ecsystem.utils.OrdersQueryCenterStation;
import com.xteam.ecsystem.utils.ReadOrCreateXLS;
import com.xteam.ecsystem.utils.StringUtil;
import com.xteam.purview.entities.PurviewUser;

/**
 * 订单信息控制层
 */
@Controller
@SuppressWarnings("unchecked")
public class OrderContorller {
	
	@Autowired
	private OrderService orderservice;
	
	public void setToBeJsonType(HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getAllOrderList.do")
	public void getAllOrderList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);

		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String direction = ReqHelper.getRequestString(request, "dir");
		String orderQueryData = ReqHelper.getRequestString(request, "orderQueryData");
		OrdersQueryCenterStation orderStation = null;

		if (StringUtil.isNotNullOrEmpty(orderQueryData)) {
			JSONObject obj = JSONObject.fromObject(orderQueryData);
			orderStation = (OrdersQueryCenterStation) JSONObject.toBean(obj,
					OrdersQueryCenterStation.class);
		}

		String strList = this.orderservice.getAllOrderList(direction, orderStation, pageIndex, pageSize);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/getOrderList.do")
	public void getOrderList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);

		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String direction = ReqHelper.getRequestString(request, "dir");
		String orderQueryData = ReqHelper.getRequestString(request, "orderQueryData");
		OrdersQueryCenterStation orderStation = null;

		if (StringUtil.isNotNullOrEmpty(orderQueryData)) {
			JSONObject obj = JSONObject.fromObject(orderQueryData);
			orderStation = (OrdersQueryCenterStation) JSONObject.toBean(obj,
					OrdersQueryCenterStation.class);
		}

		String strList = this.orderservice.getOrderList(direction, orderStation, pageIndex, pageSize);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/getRegionInfo.do")
	public void getRegionInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String regionId = ReqHelper.getRequestString(request, "regionId");		
		String strInfo = this.orderservice.getRegionInfo(regionId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getOrderInfoById.do")
	public void getOrderInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String boiId = ReqHelper.getRequestString(request, "boiId");		
		String strInfo = this.orderservice.getOrderInfoById(boiId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getMemberInfoById.do")
	public void getMemberInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bmbId = ReqHelper.getRequestString(request, "bmbId");		
		String strInfo = this.orderservice.getMemberInfoById(bmbId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getOrderGoodsById.do")
	public void getOrderGoodsById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String boiId = ReqHelper.getRequestString(request, "boiId");		
		String strInfo = this.orderservice.getOrderGoodsById(boiId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/saveDisposedOrder.do")
	public void saveDisposedOrder(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String boiId = ReqHelper.getRequestString(request, "boiId");
		String disposeType = ReqHelper.getRequestString(request, "disposeType");
		String lolUtype = ReqHelper.getRequestString(request, "operateUType");
		String operateUser = ReqHelper.getRequestString(request, "operateUser");
		String operateIp = ReqHelper.getRequestString(request, "operateIp");
		String lolOremark = ReqHelper.getRequestString(request, "lolOremark");		
		String boiCreason = ReqHelper.getRequestString(request, "boiCreason");
		String boiIsrefund = ReqHelper.getRequestString(request, "boiIsrefund");
		String boiRefundremark = ReqHelper.getRequestString(request, "boiRefundremark");
		String boiInvnum = ReqHelper.getRequestString(request, "boiInvnum");
		
		String strInfo = this.orderservice.saveDisposedOrder(boiId, disposeType, lolUtype, 
				operateUser, operateIp, lolOremark, boiCreason, boiIsrefund, boiRefundremark, boiInvnum);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/saveQueRenOrder.do")
	public void saveQueRenOrder(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		
		String boiIds = ReqHelper.getRequestString(request, "boiIds");
		String lolUtype = ReqHelper.getRequestString(request, "operateUType");
		String operateUser = ReqHelper.getRequestString(request, "operateUser");
		String operateIp = ReqHelper.getRequestString(request, "operateIp");
		
		this.orderservice.saveQueRenOrder(boiIds, lolUtype, operateUser, operateIp);
	}
	
	@RequestMapping("/saveQuXiaoOrder.do")
	public void saveQuXiaoOrder(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		
		String boiIds = ReqHelper.getRequestString(request, "boiId");
		String lolUtype = ReqHelper.getRequestString(request, "operateUType");
		String operateUser = ReqHelper.getRequestString(request, "operateUser");
		String operateIp = ReqHelper.getRequestString(request, "operateIp");
		String lolOremark = ReqHelper.getRequestString(request, "lolOremark");		
		String boiCreason = ReqHelper.getRequestString(request, "boiCreason");
		String boiIsrefund = ReqHelper.getRequestString(request, "boiIsrefund");
		String boiRefundremark = ReqHelper.getRequestString(request, "boiRefundremark");
		
		this.orderservice.saveQuXiaoOrder(boiIds, lolUtype, operateUser, operateIp, lolOremark, 
				boiCreason, boiIsrefund, boiRefundremark);
	}
	
	@RequestMapping("/delOrdersByIds.do")
	public void delOrdersByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		
		String boiIds = ReqHelper.getRequestString(request, "boiIds");
		String lolUtype = ReqHelper.getRequestString(request, "operateUType");
		String operateUser = ReqHelper.getRequestString(request, "operateUser");
		String operateIp = ReqHelper.getRequestString(request, "operateIp");
		
		this.orderservice.delOrdersByIds(boiIds, lolUtype, operateUser, operateIp);
	}
	
	@RequestMapping("/getRegionInfoList.do")
	public void getRegionInfoList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bdrId = ReqHelper.getRequestString(request, "bdrId");
		String bdrParent = ReqHelper.getRequestString(request, "bdrParent");		
		String strInfo = this.orderservice.getRegionInfoList(bdrId, bdrParent);
		
		response.getWriter().write(strInfo);
	}

	@RequestMapping("/updateConsigneeInfo.do")
	public void updateConsigneeInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String lolUtype = ReqHelper.getRequestString(request, "operateUType");
		String operateUser = ReqHelper.getRequestString(request, "operateUser");
		String operateIp = ReqHelper.getRequestString(request, "operateIp");		
		String jsonStr = ReqHelper.getRequestString(request, "orderInfo");
		JSONObject obj = JSONObject.fromObject(jsonStr);
		
		BaseOrderInfo order = (BaseOrderInfo)JSONObject.toBean(obj, BaseOrderInfo.class);
		int count = this.orderservice.updateConsigneeInfo(order, lolUtype, operateUser, operateIp);
		
		response.getWriter().write(count);
	}
	
	@RequestMapping("/findOrderList.do")
	public void findOrderList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String boiConsignee = ReqHelper.getRequestString(request,
				"boiConsignee");
		String shopname = ReqHelper.getRequestString(request, "shopname");
		String boiMember = ReqHelper.getRequestString(request, "boiMember");
		String boiMobile = ReqHelper.getRequestString(request, "boiMobile");
		String boiShip = ReqHelper.getRequestString(request, "boiShip");
		String boiOrder = ReqHelper.getRequestString(request, "boiOrder");
		String boiOtimes = ReqHelper.getRequestString(request, "boiOtimes");
		String boiOtimee = ReqHelper.getRequestString(request, "boiOtimee");
		String str = orderservice.findOrderList(pageIndex, pageSize,
				boiConsignee, shopname, boiMember, boiMobile, boiOrder,
				boiOtimes, boiOtimee,boiShip);
		response.getWriter().write(str);
	}

	@RequestMapping("/printOrderList.do")
	public String printOrderList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String orderList = ReqHelper.getRequestString(request, "orderList");
		String c[] = orderList.split("\\|");
		List list = orderservice.queryHqlIn(Arrays.asList(c));
		request.setAttribute("orderlist", list);
		return "ordersMgr/printOrder";

	}

	@RequestMapping("/exportOrderXls.do")
	public void exportOrderXls(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String boiConsignee = ReqHelper.getRequestString(request,
				"boiConsignee");
		String shopname = ReqHelper.getRequestString(request, "shopname");
		String boiMember = ReqHelper.getRequestString(request, "boiMember");
		String boiMobile = ReqHelper.getRequestString(request, "boiMobile");
		String boiOrder = ReqHelper.getRequestString(request, "boiOrder");
		String boiOtimes = ReqHelper.getRequestString(request, "boiOtimes");
		String boiOtimee = ReqHelper.getRequestString(request, "boiOtimee");
		String boiShip = ReqHelper.getRequestString(request, "boiShip");
		List list = orderservice.findExportOrder(boiConsignee, shopname,
				boiMember, boiMobile, boiOrder, boiOtimes, boiOtimee,boiShip);
	/*	String title[] = { "订单编号", "订购人手机号", "订购人姓名", "收货人手机号", "收货人名", "订购时间",
				"收货地址", "订单总价", "订购状态", "产品明细" };*/
		String title[] = { "订单编号" , "收货人名", "收货人手机号","订购时间",
				"收货地址", "订单总价", "订购状态","配送状态", "产品明细" };
		OutputStream os = response.getOutputStream();// 取得输出流
		response.reset();// 清空输出流
		response.setHeader("Content-disposition",
				"attachment; filename=orderList.xls");// 设定输出文件头
		response.setContentType("application/msexcel");// 定义输出类型
		ReadOrCreateXLS.exportExcel(os, list, title, "商品订单", null, "order");
	}

	@RequestMapping("/findOrderProductList.do")
	public void findOrderProductList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String orderid = ReqHelper.getRequestString(request, "orderid");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String str = orderservice.findOrderProductList(pageIndex, pageSize,
				orderid);
		response.getWriter().write(str);
	}

	@RequestMapping("/saveOptionOrder.do")
	public void saveOptionOrder(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "orderInfo");
		JSONObject obj = JSONObject.fromObject(jsonStr);
		String optionState = obj.get("optionState").toString();
		String currentState = obj.get("currentState").toString();
		String optionChange = obj.get("optionChange").toString();
		String boiId = obj.get("boiId").toString();
		PurviewUser user=(PurviewUser)request.getSession().getAttribute("userId");
		orderservice.updateOrderState(boiId, currentState, optionState,optionChange,request.getLocalAddr(),user.getUname());
	}
	
	@RequestMapping("/getGoodsCombobox.do")
	public void getGoodsCombobox(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String value= ReqHelper.getRequestString(request, "value");
		String str = orderservice.getGoodsCombobox(value);
		response.getWriter().write(str);
	}
	
	@RequestMapping("/showGoodsInfoById.do")
	public void showGoodsInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgiId= ReqHelper.getRequestString(request, "bgiId");
		String str = orderservice.showGoodsInfoById(bgiId);
		response.getWriter().write(str);
	}
	
	@RequestMapping("/showGoodsAttrById.do")
	public void showGoodsAttrById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgiId= ReqHelper.getRequestString(request, "bgiId");
		String str = orderservice.showGoodsAttrById(bgiId);
		response.getWriter().write(str);
	}
	
	@RequestMapping("/saveOrderGoods.do")
	public void saveOrderGoods(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "orderData");
		String boiGpay = ReqHelper.getRequestString(request, "boiGpay");
		String boiId = ReqHelper.getRequestString(request, "boiId");
		String lolUtype = ReqHelper.getRequestString(request, "operateUType");
		String operateUser = ReqHelper.getRequestString(request, "operateUser");
		String operateIp = ReqHelper.getRequestString(request, "operateIp");
		
		orderservice.saveOrderGoods(jsonStr,boiGpay, boiId , lolUtype, operateUser, operateIp);
	}
}
